<?php namespace App\Http\Controllers\Site;
use App;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
//use App\Http\Requests\Request;
use App\Item;
use App\Product;
use App\Type;
use App\Messagebox;
use Illuminate\Support\Facades\Cache;
use DB;

use Cookie;
use Illuminate\Support\Facades\Response;

class IndexController extends Controller
{
    public function getIndex()
    {
//        $items = App::environment('staging');
//        print_r($types);
//        exit();

        $parents = Type::where('parent_id', 0)->orderBy('range','ASC')->get();
        $products = Product::orderBy('RCMD', 'DESC')->orderBy('hot', 'DESC')->orderBy('updated_at', 'DESC')->take(9)->get();
        $indexswiper = Cache::get('indexswiper');


        return view('site.index', [
            'parents' => $parents,
            'products' => $products,
            'indexswiper'=> $indexswiper,
            'pageName' => 'index',
        ]);
    }

    public function getLang($lang)
    {
        if ($lang != Cookie::get('lang')) {
            Cache::flush();
        }
        if ($lang != 'zh_cn') {
            $lang = 'en';
        }
        Cookie::queue('lang', $lang, 200);
        return redirect()->guest('/index');
    }

    public function getAbout($id = 0)
    {
        //$abouts = Item::where('type', 'about')->where('lang', $this->setLang())->lists('name', 'id');
        $model = Item::where('type', 'about')->where('lang', $this->setLang())->get();
        if (0 == $id) {
            foreach ($model as $key => $about) {
                $id = $key;
                break;
            }
        }
        return view('site.about', [
            'model' => $model,
            'pageName' => 'about',
            'id' => $id,
        ]);
    }

    public function getService()
    {
        $model = Item::where('type', 'service')->where('lang', $this->setLang())->orderBy('created_at', 'DESC')->first();
        return view('site.service', [
            'model' => $model,
            'pageName' => 'service',
        ]);
    }

    public function getCooperate()
    {
        $model = Item::where('type', 'cooperate')->where('lang', $this->setLang())->orderBy('created_at', 'DESC')->first();
        return view('site.cooperate', [
            'model' => $model,
            'pageName' => 'cooperate',
        ]);
    }

    public function getEvents($id = 0)
    {
        if (Cache::has('events')) {
            $events = Cache::get('events');
        } else {
            $navs = Cache::get('nav');
            $eventsQuerys = Item::where('type', 'event')->where('lang', $this->setLang())->orderBy('name', 'DESC')->select('name', 'id', 'kind')->get();

            foreach ($navs['event']['list'] as $key => $nav) {
                $events[$key]['name'] = $nav;
                $events[$key]['data'] = [];
                foreach ($eventsQuerys as $eventsQuery) {
                    if ($eventsQuery->kind == $key) {
                        $events[$key]['data'][$eventsQuery->id] = $eventsQuery->name;
                    }
                }
            }

        }
        if (0 == $id) {
            $model = DB::select('select a.*,b.name as currentYear from items a left join items b on a.kind=b.id where a.`type`="event" order by created_at desc limit 1');
        } else {
            $model = DB::select('select a.*,b.name as currentYear from items a left join items b on a.kind=b.id where a.id='.$id);
        }
      // print_r($model); exit();
        $currentYear = $model[0]->currentYear;
        return view('site.event', [
            'events' => $events,
            'model' => $model[0],
            'currentYear' => $currentYear,
            'id' => $id,
            'list' => pos($events)['data'],
            'pageName' => 'event',
        ]);
    }

    public function getContact($id=0)
    {
        return view('site.contact', [
            'id'=>$id,
            'pageName' => 'contact',
            'lang' => $this->setLang()
        ]);
    }


    public  function postCommitmessage(Request $request)
    {
        $data = $request->all();
        $result = Messagebox::create([
                'company' => $data['company'],
                'name' => $data['name'],
                'tel' => $data['tel'],
                'email' => $data['email'],
                'subject' => $data['subject'],
                'content' => $data['content'],
            ]
        );
        if ($result) {
            $ret = ['state' => 'success', 'message' => 'success!'];
        } else {
            $ret = ['state' => 'faile', 'message' => 'faile!'];
        }
        return \Response::json($ret);
    }

    public function postMessage(Request $request , $id = 0)
    {
        $data = $request->all();
        $datas = [];
        $attributes = [
            'company' => trans('base.Name of company'),
            'name' => trans('base.Name'),
            'tel' => trans('base.TEL'),
            'email' => trans("base.E-mail"),
            'subject' => trans("base.Subject"),
            'content' => trans("base.Content")
        ];
        $rules = [
            'company' => "required|min:2|max:15",
            'name' => "required|min:2|max:15",
            'tel' => "required|numeric|min:11|max:11",
            'email' => "required|email",
        ];
        if('zh_cn' == parent::setLang()){
            $validator = \Validator::make($data, $rules,
                [],
                $attributes);
        } else {
            $validator = \Validator::make($data, $rules,
                [],
                $attributes);
        }
        if ($validator->fails()) {
            $message = $validator->messages();
            $ret['message'] = $message;
            $ret['state'] = 'fail';
            foreach ($attributes as $k => $attr) {
                if ($message->has($k)) {
                    $ret['message'] = $message->first($k);
                    break;
                }
            }
        } else {
            $result = Messagebox::create([
                    'company' => $data['company'],
                    'name' => $data['name'],
                    'tel' => $data['tel'],
                    'email' => $data['email'],
                    'subject' => $data['subject'],
                    'content' => $data['content'],
                ]
            );
            if ($result) {
                $ret = ['state' => 'success', 'message' => 'success!'];
            } else {
                $ret = ['state' => 'faile', 'message' => 'faile!'];
            }
        }
        return \Response::json($ret);
    }

    public function getTest()
    {
        $items = config('site.items');
        foreach ($items as $itemName => $item) {
            $navs[$itemName]['nav'] = $item['nav'];
            $navs[$itemName]['name'] = $item['name'];
            $navs[$itemName]['itemName'] = $itemName;
            $navs[$itemName]['nav'] = $item['nav'];

            if (1 == $item['nav']) {
                if (isset($item['type'])) {
                    if ($item['data'][$item['type']]['type'] == "dynamic") {
                        $navs[$itemName]['list'] = Item::where('type', $item['data'][$item['type']]['value'])->lists('name', 'id');
                    } else {
                        $navs[$itemName]['list'] = $item['data'][$item['type']]['value'];
                    }
                } else {
                    $navs[$itemName]['list'] = Item::where('type', $itemName)->lists('name', 'id');
                }
            }
        }
        dd($navs);
    }
}